package ssm.controller;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


import ssm.entity.Shop;

import ssm.service.ShopService;



@Controller
public class ShopController {
	
	//属性注入
	@Autowired
	private ShopService shopService;
	public ShopService getShopService() {
		return shopService;
	}
	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}
	
	//商店列表
	@RequestMapping("/shop_list")
	public ModelAndView shop_list() {
		List<Shop> shops=shopService.getList();
		ModelAndView mView =new ModelAndView("shop/shop_list");
		mView.addObject("shops",shops);
		return mView;
		
	}
	
	//商店详情
	@RequestMapping("/shop_detail")
	public ModelAndView shop_detail(int shop_id) {
		Shop shop = shopService.getDetail(shop_id);
		ModelAndView mView =new ModelAndView("shop/shop_detail");
		mView.addObject("shop",shop);
		return mView;
		
	}
	
	
	//添加商店页面
	@RequestMapping("/shop_add")
	public ModelAndView shop_add() {
		ModelAndView mView =new ModelAndView("shop/shop_add");
		return mView;
		
	}
	

    //添加商店逻辑
    @RequestMapping(value = "/add_shop" ,method = RequestMethod.POST)
    @ResponseBody
    public String addShop(MultipartFile file,Shop shop,HttpServletRequest request) throws IOException{
    	//添加图片
    	//保存图片地址到/imgs/
    	String realPath = request.getServletContext().getRealPath("/");//获取文件物理地址
    	System.out.println("物理地址："+realPath);
    	String filename = file.getOriginalFilename();//获取图片名字
    	System.out.println("图片名字："+filename);
        long l = System.currentTimeMillis();//用时间代替原名字
        String newname = l + filename;//新名字
        System.out.println("新名字："+newname);
    	String path = realPath+"/imgs/"+newname;
    	System.out.println("图片地址："+path);//地址
    	//通过给定路径创建file实例将上传的文件传输到File中
    	File f = new File(path);
    	//将上传的文件传输到File中
    	file.transferTo(f);
    	//文件名保存
    	shop.setShop_picture(newname);
    	//添加所有信息到数据库
    	String msg=shopService.shop_add(shop);
    	return msg;
    }
	
	
	//商店修改页面
	@RequestMapping("/shop_edit")
	public ModelAndView shop_edit(int shop_id) {
		Shop shop = shopService.getDetail(shop_id);
		ModelAndView mView =new ModelAndView("shop/shop_edit");
		mView.addObject("shop",shop);
		return mView;
		
	}
	
	//商店修改
	@RequestMapping("/edit_shop")
	@ResponseBody
	public String editShop(Shop shop) {
		String content=shopService.shop_edit(shop);
		return content;
		
	}
	

	//删除商店
	@RequestMapping("/delShop")
	@ResponseBody
	public String delShop(int shop_id) {
		String content = shopService.shop_delete(shop_id);
		return content;
	}
	
	
	//小程序添加商店
	@RequestMapping("/addshop")
	@ResponseBody
	public String addshop(@RequestParam("shop_name") String shop_name,@RequestParam("shop_address") String shop_address,@RequestParam("shop_type") String shop_type) {
		System.out.println(shop_name+shop_address+shop_type);
		Shop shop = new Shop();
		shop.setShop_name(shop_name);
		shop.setShop_address(shop_address);
		shop.setShop_type(shop_type);
		shopService.addshop(shop);
		return "";
	}
	
	//小程序商店列表
	@RequestMapping("shoplist")
	@ResponseBody
	public Map<String,Object> shoplist(){
		Map<String,Object> ret = new HashMap<String, Object>();
		System.out.println("ret "+ret.size());
		List<Shop> shoplist = shopService.getList();
		ret.put("shoplist", shoplist);
		return ret;
	}
	
	


}










